const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    width:'150rpx',



    getcode:'获取验证码',
    getcodestatus:false,

    sexarr: ['女', '男'],
    sexindex:0,
    //学校列表
    schoollist:[],
    // 班级列表
    classlist:[],
    // 学校搜索框状态
    schoolstatus:false,
    // 班级input状态
    classstatus:false,
    // 搜索完后选择的学校信息
    schoolvalue:'',
    index:'',

    // 选择的班级信息
    classname:'',


    username:'',
    phone:'',
    code: '',
    pwd:'',
    school_id:'',
    classs_id:'',
    name:'',
    sex:'女'
  },
  
  // 录入选择班级信息
  selectclass(e){
    var that = this
    console.log(e)
    this.setData({
      classname: e.currentTarget.dataset.classname,
      classs_id: e.currentTarget.dataset.classid
    })
  },
  // 班级输入框聚焦
  classinputfocus:function(){
    this.setData({
      classstatus: true
    })
  },
  // 班级输入框失去焦点
  classinputblur: function () {
    let that =this
    setTimeout(function () {
      that.setData({
        classstatus: false
      })
    }, 1)
    
  },
  // 选择学校信息
  selectschool:function(e){
    var that =this
    console.log(e)
    that.setData({
      schoolvalue: e.currentTarget.dataset.schoolname,
      school_id: e.currentTarget.dataset.schoolid,
      index: e.currentTarget.dataset.index,
      classlist: that.data.schoollist[e.currentTarget.dataset.index].classs,
      schoolstatus:false,
    })
    console.log(that.data.classlist)
  },
  // 学校搜索框失去焦点
  schoolblur:function(){
    var that = this
    setTimeout(function(){
      that.setData({
        schoolstatus: false,
      })
    },100)
    
  },
  // 搜索学校
  schoolsearch:function(e){
    var that = this
    console.log(e)
    wx.request({
      url: app.api + app.pschool,
      data:{
        schoolname:e.detail.value
      },
      method: 'POST',
      success: function(res) {
        console.log(res)
        if(res.data.code == 0){
          that.setData({
            schoolstatus:true,
            schoollist:res.data.data.list
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration:2000
          })
        }
      },
    })
  },
  // 点击注册
  register:function(){
    var that = this

    if (!that.data.school_id){
      wx.showToast({
        title: '学校不能为空',
        icon:'none',
        duration:2000
      })
      return false
    }

    if (!that.data.classs_id) {
      wx.showToast({
        title: '学校不能为空',
        icon: 'none',
        duration: 2000
      })
      return false
    }

    if (!that.data.name) {
      wx.showToast({
        title: '请输入正确姓名',
        icon: 'none',
        duration: 2000
      })
      return false
    }

    let rule = /^1[123456789]\d{9}$/
    if (!rule.test(that.data.phone)) {

      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (that.data.pwd.length<6) {
      wx.showToast({
        title: '请输入6~12位密码',
        icon: 'none',
        duration: 2000
      })
      return false
    }



    let setdata = {
      username:that.data.username,
      phone: that.data.phone,
      code: that.data.code,
      pwd: that.data.pwd,
      school_id: that.data.school_id,
      classs_id: that.data.classs_id,
      name: that.data.name,
      sex: that.data.sex,
    }
    console.log(setdata)
    wx.request({
      url: app.api + app.pregister,
      method:'POST',
      data:setdata,
      success(e){
        console.log(e);
        if(e.data.code==0){
          wx.setStorage({
            key: 'parentuser',
            data: e.data.data,
            success(){
              wx.showToast({
                title: '注册成功',
                icon:'none',
                duration:2000,
                success(){
                  wx.navigateTo({
                    url: '../Alogin/index',
                  })
                }
              })
            }
          })

        }else{
          wx.showToast({
            title: e.data.msg,
            icon:'none',
            duration:2000
          })
        }
      }
    })
  },
  // 点击获取验证码
  gocode:function(){
    
    let that = this
    let rule = /^1[123456789]\d{9}$/
    if (!rule.test(that.data.phone)) {

      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    // 防止多次点击触发
    if (that.data.getcodestatus) {
      return false
    }
    wx.request({
      url: app.api + app.pregister_yzm,
      method:'POST',
      data:{
        phone:that.data.phone
      },
      success(res){
        console.log(res)
        if(res.data.code == 0){
          that.setData({
            width: '52rpx'
          })
          wx.showToast({
            title: res.data.msg,
            icon:'success',
            duration:2000
          })


          let s = 61
          s--
          that.setData({
            getcode: s + 's'
          })
          let timer = setInterval(() => {
            s--
            that.setData({
              getcode: s + 's',
              getcodestatus: true,
            })
            if (s <= 0) {
              clearInterval(timer)
              that.setData({
                getcode: '获取验证码',
                getcodestatus: false,
                width: '150rpx'
              })
            }
          }, 1000)




        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration:2000
          })
        }
      }
    })



    
  },
  username: function (e) {
    console.log(e)
    var that = this
    that.setData({
      username: e.detail.value
    })
  },
  name: function (e) {
    console.log(e)
    var that = this
    that.setData({
      name: e.detail.value
    })
  },
  // 手机号
  phone:function(e){
    console.log(e)
    var that = this
    that.setData({
      phone:e.detail.value
    })
  },
  code:function (e) {
    console.log(e)
    var that = this
    that.setData({
      code: e.detail.value
    })
  },
  pwd: function (e) {
    console.log(e)
    var that = this
    that.setData({
      pwd: e.detail.value
    })
  },
  // 性别
  sex:function(e){
    var that = this
    console.log(e)
    that.setData({
      sexindex:e.detail.value,
      sex: that.data.sexarr[e.detail.value]
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})